<?php
namespace app\common\service\impl;

use app\common\enums\ResultEnum;
use app\common\exception\ZeduException;
use app\common\model\DetailsLog;
use app\common\model\RentDetails;
use app\common\service\CommonService;
use app\common\service\RentDetailsService;
use think\facade\Log;

class RentDetailsServiceImpl implements RentDetailsService
{
    protected $rentDetails;
    protected $detailsLog;
    protected $commonService;
    
    public function __construct(RentDetails $rentDetails,DetailsLog $detailsLog,CommonService $commonService)
    {
        
        $this->rentDetails = $rentDetails;
        $this->detailsLog = $detailsLog;
        $this->commonService = $commonService;
    }
    
    public function edit(array $data)
    {
        $value = $this->rentDetails->where(['id'=>$data['id']])->value($data['key']);
        
        $dateArr = ['start_time','end_time','invoice_time','ought_time','actual_time','expire_time','contract_time'];
        if(in_array($data['key'],$dateArr)){
            $value = date('Y-m-d',$value);
        }
        
        if($data['key'] == 'belongs_month'){
            $value = date('Y-m',$value);
        }
        
        $this->rentDetails->startTrans();
        try{
            
            $this->rentDetails->save([$data['key']=>$data['value']],['id'=>$data['id']]);
            
            if($this->rentDetails->update_time){
                $model = $this->recordLog($data,$value,$this->rentDetails[$data['key']]);
                if($model->id){
                    $this->rentDetails->commit();
                    return $this->rentDetails;
                }else{
                    $this->rentDetails->rollback();
                    return false;
                }
 
            }else{
                $this->rentDetails->rollback();
                return false;
            }
            
        }catch(\Exception $e){
            $this->rentDetails->rollback();
            Log::record('[ 收款明细表更新数据 ]:'.$e->getMessage(),'error');
            return false;
        }

    }
    
    public function index(array $where, int $page, int $length)
    {
        try {
            $model = RentDetails::with('agency,user,brand')->where($where)->order('id', 'desc')
            ->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            
        } catch (\Exception $e) {
            Log::record('[ 长租明细列表 ]:'.$e->getMessage(),'error');
            return false;
        }
        return $model;
    }
    
    public function uploadFile(array $data)
    {
        $uploadData = [];
        $createData = [];
        $user = [];
        $agency = [];
        $brand = [];
        foreach ($data as $value) {
           
            $user['name'] = $value[0]??'';
            $uploadData['user_id'] = $this->commonService->findUser($user);
            $agency['name'] = $value[1]??'';
            $uploadData['agency_id'] = $this->commonService->findAgency($agency);
            $brand['name'] = $value[2]??'';
            $uploadData['brand_id'] = $this->commonService->findBrand($brand);
            $contractNo = $value[3];
            $uploadData['contract_number'] =$this->findContractNo($contractNo);
            $type = $value[4]??'';
            switch ($type) {
                case '长租':
                    
                    $uploadData['type'] = 1;
                    break;
                case '以租代购':
                    
                    $uploadData['type'] = 2;
                    break;
                case '共享车主':
                    
                    $uploadData['type'] = 3;
                    break;
                case '短租':
                    
                    $uploadData['type'] = 4;
                    break;
                default:
                    $uploadData['type'] = null;
                    break;
            }
            $uploadData['rent_money'] = $value[5]??'';
            $uploadData['service_money'] = $value[6]??'';
            $uploadData['rent_price'] = $value[7]??'';
            $uploadData['number'] = $value[8]??'';
            $uploadData['back_car_number'] = $value[9]??'';
            $uploadData['reduce_money'] = $value[11]??'';
            $uploadData['contract_money'] = $value[12]??'';
            $uploadData['offsets_money'] = $value[13]??'';
            $uploadData['actual_money'] = $value[15]??'';
            $uploadData['belongs_month'] = $value[17]??'';
            $uploadData['start_time'] = $value[18]??'';
            $uploadData['end_time'] = $value[19]??'';
            $uploadData['notes'] = $value[20]??'';
            $uploadData['invoice_time'] = $value[21]??'';
            $uploadData['rent_man'] = $value[22]??'';
            $uploadData['contract_time'] = $value[23]??'';
            $uploadData['expire_time'] = $value[24]??'';
            $uploadData['rent_number'] = $value[25]??'';
            $uploadData['ought_time'] = $value[26]??'';
            $uploadData['actual_time'] = $value[27]??'';
            $uploadData['pay_type'] = $value[29]??'';
            $uploadData['deposit'] = $value[30]??'';
            $uploadData['deposit_number'] = $value[31]??''; 
            $uploadData['status'] = 1;
            $createData[] = $uploadData;
        }

        try{
            
            $model = $this->rentDetails->saveAll($createData);
           
        }catch(\Exception $e){
            Log::record('[ 导入数据 ]:'.$e->getMessage(),'error');
            return false;
        }
        
        return $model;
    
    }
    
    public function findContractNo($param){
        if($param){
            return $param;
        }else{
            throw new ZeduException(['code'=>ResultEnum::FIND_ERROR['code'],'msg'=>'合同编号不能为空']);
        }
    }

    public function delete(int $id)
    {
        try {
            $this->rentDetails->destroy($id);
        } catch (\Exception $e) {
            Log::record('[ 删除数据 ]:'.$e->getMessage(),'error');
            return false;
        }
        return true;
        
    }

    public function recordLog($data,$oldValue,$value){
        $type = [
            'offsets_money'=>1,
            'actual_money'=>2,
            'belongs_month'=>3,
            'start_time'=>4,
            'end_time'=>5,
            'notes'=>6,
            'invoice_time'=>7,
            'rent_man'=>8,
            'rent_number'=>9,
            'ought_time'=>10,
            'actual_time'=>11,
            'pay_type'=>12,
            'deposit'=>13,
            'deposit_number'=>14,
            'expire_time'=>15,
            'contract_time'=>16,
            'reduce_money'=>17,
            'back_car_number'=>18
        ];
        $log = ['details_id'=>$data['id'],'admin_id'=>session('admin.id'),'type'=>$type[$data['key']],'old_value'=>$oldValue,'value'=>$value];
        
        try {
            $model = $this->detailsLog->create($log);
            
        } catch (\Exception $e) {
            Log::record('[ 记录日志 ]:'.$e->getMessage(),'error');
            return false;
        }
        return $model;
    }
    
    
    public function rentLog(array $data)
    {
        try {
            $model = $this->detailsLog::with('admin')->where(['details_id'=>$data['id']])->order('id', 'desc')
            ->paginate($data['length'],false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$data['page'],
            ]);
        } catch (\Exception $e) {
            Log::record('[ 查询日志 ]'.$e->getMessage(),'error');
            return false;
        }
        
        return $model;
    }



}

